package nina.yiyou.salary.input.config

import groovy.transform.PackageScope
import nina.yiyou.salary.input.util.CellValueGetter
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook

@PackageScope
class GlobalSheetReader {

  GlobalSheetReader(Workbook excel) {
    _excel = excel
  }

  Map<String, String> read() {
    Sheet sheet = _excel.getSheetAt(0)
    def getCell = { Row row -> new CellValueGetter(row.getCell(1)).get() }

    return (0..sheet.lastRowNum)
        .collect { sheet.getRow(it) }
        .collectEntries { [it.getCell(0).stringCellValue, getCell(it)] }
  }

  private final Workbook _excel
}
